package cn.com.inossem.operation;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.os.Bundle;
import android.os.Parcelable;
import cn.com.inossem.config.WSConfig;
import cn.com.inossem.model.ProjectGongsiInfoBean;
import cn.com.inossem.network.NetworkConnect;
import cn.com.inossem.requestmanager.WooRequestFactory;

import com.foxykeep.datadroid.exception.ConnectionException;
import com.foxykeep.datadroid.exception.CustomRequestException;
import com.foxykeep.datadroid.exception.DataException;
import com.foxykeep.datadroid.requestmanager.Request;
import com.foxykeep.datadroid.service.RequestService.Operation;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

public class ProjectGongsiOperation implements Operation {

	public Bundle bundle;

	@Override
	public Bundle execute(Context arg0, Request request)
			throws ConnectionException, DataException, CustomRequestException {

		NetworkConnect connection = new NetworkConnect(WSConfig.LOCAL_URL
				+ "userInfo/findUserExpCom");
		connection.setPostText("{\"username\":\"" + "" + "\",\"password\":\""
				+ "" + "\"}");
		System.out.println("{\"username\":\"" + "" + "\",\"password\":\"" + ""
				+ "\"}");
		String body = connection.execute();

		System.out.println("ProjectGongsiOperation:" + body);
		bundle = new Bundle();
		try {
			JSONObject jsonObject = new JSONObject(body);
			int errorFlag = jsonObject.getInt("errorFlag");

			List<ProjectGongsiInfoBean> retList;
			if (errorFlag == WSConfig.RESULT_SUCESS) {

				Gson datagson = new Gson();
				String listUserExpCom = jsonObject.getString("listUserExpCom");
				retList = datagson.fromJson(listUserExpCom,
						new TypeToken<List<ProjectGongsiInfoBean>>() {
						}.getType());
				System.out.println("retList.size():" + retList.size());

			} else {
				retList = null;
			}
			// 返回返回值
			bundle.putParcelableArrayList(
					WooRequestFactory.RESPONSE_PROJECTGONGSI_DATA,
					(ArrayList<? extends Parcelable>) retList);
			// 设置结果
			bundle.putInt(WooRequestFactory.BUNDLE_EXTRA_PROJECTSUCCESS,
					errorFlag);
		} catch (JSONException e) {
			e.printStackTrace();
		}

		return bundle;
	}
}
